//
//  QGRegistViewController.m
//  QuanGou
//
//  Created by qqqq on 15/11/18.
//  Copyright © 2015年 DongYongFei. All rights reserved.
//

#import "QGRegistViewController.h"
#import "QGRegistCodeViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import <PhotoTweaksViewController.h>


@interface QGRegistViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,PhotoTweaksViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;

@end

@implementation QGRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.photoImageView.layer.cornerRadius = 50;
    self.photoImageView.userInteractionEnabled = YES;
    self.photoImageView.clipsToBounds = YES;
    self.photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    UITapGestureRecognizer *tapPhoto = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGuestureClick:)];
    [self.photoImageView addGestureRecognizer:tapPhoto];
    UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewClick:)];
    [self.view addGestureRecognizer:tapView];
    
    

}
- (IBAction)getSecurityCode:(id)sender {
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNumberTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
           
            QGRegistCodeViewController *registCode = [[QGRegistCodeViewController alloc] init];
            UINavigationController *registNC = [[UINavigationController alloc] initWithRootViewController:registCode];
           
            registCode.password = self.passwordTextField.text;
            registCode.phoneNumber = self.phoneNumberTextField.text;
            registCode.userico = self.photoImageView.image;
            [self showViewController:registNC sender:nil];
        }else {
            UIAlertController *errorController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的手机号" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
            [errorController addAction:action];
            [self presentViewController:errorController animated:YES completion:nil];
        }
    }];
    
}

- (void)tapViewClick:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}
- (void)photoTweaksController:(PhotoTweaksViewController *)controller didFinishWithCroppedImage:(UIImage *)croppedImage{
    
    self.photoImageView.image = croppedImage;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoTweaksControllerDidCancel:(PhotoTweaksViewController *)controller {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)tapGuestureClick:(UITapGestureRecognizer *)tap {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *phontoListAction = [UIAlertAction actionWithTitle:@"从相册选取" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        
        [self showDetailViewController:imagePicker sender:nil];

    }];
    UIAlertAction *camereAction = [UIAlertAction actionWithTitle:@"从相机拍照选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        
        [self showDetailViewController:imagePicker sender:nil];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    
    [alertController addAction:phontoListAction];
    [alertController addAction:camereAction];
    [alertController addAction:cancleAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    PhotoTweaksViewController *photoTweaksViewController = [[PhotoTweaksViewController alloc] initWithImage:image];
    photoTweaksViewController.delegate = self;
    [picker presentViewController:photoTweaksViewController animated:YES completion:nil];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
